/**
 @file: DomainId.java
 
 PURPOSE:
 This class represents an instance of a DomainId in a Domain Definition.
 
 @author Ron Williams
 
 REVISION HISTORY ABSTRACT
 
 PROGRAMMER:  Ron Williams ORG: MSE
 
 Copyright 2011 Mission Solutions LLC. All rights reserved.
 
 END ABSTRACT
 */

package com.mse.utilities;

import java.lang.Integer;
import java.lang.String;
import java.lang.StringBuilder;

import javax.swing.JOptionPane;

import com.mse.utilities.DomainDefLimits;
import com.mse.utilities.DomainException;

/**
 DESCRIPTION:
 The DomainId class contains the information as a result of parsing a
 Domain Definition file for a DomainId definition. The attributes for the
 DomainId are user defined. However the DomainType must be either logical
 or physical. (we ignore case for this value)
 */
public class DomainId {
	
	/**
	 DESCRIPTION:
	 Create a DomainId object.
	 @return void
	 @throws n/a
	 */
	public DomainId() {
		this.DID         = "";
		this.domainType  = "";
		this.displayName = "";
		this.version     = "";
		this.major       = 0;
		this.minor       = 0;
		this.isLogical   = true;      // assume logical as default
	}
	
	/**
	 DESCRIPTION:
	 Create a DomainId object based on specified attributes
	 @param did      - string containing the Domain Identifier
	 @param domType  - string identifying the type of domain (logical/physical)
	 @param dispName - string for a displayable name for this domain
	 @param version  - string identifying version of the domain definition
	 @param major    - string holding the major version integer number
	 @param minor    - string holding the minor version integer number
	 @return void
	 @throws DomainException - if the domType is not either "logical" or
	 "physical"
	 */
	public DomainId(String did, String domType, String dispName, String version,
					String major, String minor) throws DomainException {
		boolean ok = false;     // assume inputs is bad
		
		if ("logical".equals(domType.toLowerCase()) ) {
			isLogical = true;
			ok        = true;
		} else if ("physical".equals(domType.toLowerCase()) ) {
			isLogical = false;
			ok        = true;
		}
		
		if (ok) {
			// verify input string sizes are not too long
			if (did.length() > DomainDefLimits.DID_DID_Max) {
				JOptionPane.showMessageDialog(null, "DomainID, DID field value is too " +
						  							"large, can not exceed length: " +
						  							DomainDefLimits.DID_DID_Max);
				throw new DomainException("DomainID, DID field value is too " +
										  "large, can not exceed length: " +
										  DomainDefLimits.DID_DID_Max);
			}
			if (dispName.length() > DomainDefLimits.DID_DispName_Max) {
				JOptionPane.showMessageDialog(null, "DomainID, DisplayName field value is " +
						  							"too large, can not exceed length: " +
						  							DomainDefLimits.DID_DispName_Max);
				throw new DomainException("DomainID, DisplayName field value is " +
										  "too large, can not exceed length: " +
										  DomainDefLimits.DID_DispName_Max);
			}
			if (version.length() > DomainDefLimits.DID_Version_Max) {
				JOptionPane.showMessageDialog(null, "DomainID, version field value is " +
						  							"too large, can not exceed length: " +
						  							DomainDefLimits.DID_Version_Max);
				throw new DomainException("DomainID, version field value is " +
										  "too large, can not exceed length: " +
										  DomainDefLimits.DID_Version_Max);
			}
			if(Integer.parseInt(major)<0 || Integer.parseInt(minor)<0){
				JOptionPane.showMessageDialog(null, "DomainID has Invalid integer value " +
				  									"for major/minor tags");
				throw new DomainException("DomainID has Invalid integer value " +
										  "for major/minor tags");
			}
			
			this.DID         = did;
			this.domainType  = domType;
			this.displayName = dispName;
			this.version     = version;
			this.major = Integer.parseInt(major);
			this.minor = Integer.parseInt(minor);
		} else {
			throw new DomainException("DomainID has Invalid value for DomainType,"                          + " it must be either \"physical\" or \"logical\"");
		}
	}
	
	/**
	 DESCRIPTION:
	 get the Domain Identifier
	 @return did - the domain identifier
	 @throws n/a
	 */
	public String getDID() {
		return DID;
	}
	
	/**
	 DESCRIPTION:
	 set the Domain Identifier
	 @param did - the domain identifier
	 @throws n/a
	 */
	public void setDID(String did) {
		this.DID = did;
	}
	
	/**
	 DESCRIPTION:
	 Obtain indicator of the kind of domain this DomainId is part of.
	 @return true/false - true means logical domain, false means physical
	 @throws n/a
	 */
	public boolean isDomainLogical() {
		return this.isLogical;
	}
	
	/**
	 DESCRIPTION:
	 get the domain type string
	 @return domType - the domain type (logical/physical)
	 @throws n/a
	 */
	public String getDomainType() {
		return domainType;
	}
	
	/**
	 DESCRIPTION:
	 set the domain type. (must be either logical or physical)
	 @param domType - the type of domain definition
	 @throws n/a
	 */
	public void setDomainType(String domType) throws DomainException {
		
		if ("logical".equals(domType.toLowerCase()) ) {
			isLogical = true;
		} else if ("physical".equals(domType.toLowerCase()) ) {
			isLogical = false;
		} else {
			throw new DomainException("Invalid value for DomainType,"                          + " it must be either \"physical\" or \"logical\"");
		}
		this.domainType = domType;
	}
	
	/**
	 DESCRIPTION:
	 get the display name
	 @return name - the display name for this DomainId
	 @throws n/a
	 */
	public String getDisplayName() {
		return displayName;
	}
	
	/**
	 DESCRIPTION:
	 set the display name
	 @param name - the display name for this DomainId
	 @throws n/a
	 */
	public void setDisplayName(String name) {
		this.displayName = name;
	}
	
	/**
	 DESCRIPTION:
	 get the version
	 @return version - the version for this DomainId
	 @throws n/a
	 */
	public String getVersion() {
		return version;
	}
	
	/**
	 DESCRIPTION:
	 set the version
	 @param version - the version for this DomainId
	 @throws n/a
	 */
	public void setVersion(String version) {
		this.version = version;
	}
	
	/**
	 DESCRIPTION:
	 get the major number
	 @return major - the major number for this DomainId
	 @throws n/a
	 */
	public int getMajor() {
		return major;
	}
	
	/**
	 DESCRIPTION:
	 get the minor number
	 @return minor - the minor number for this DomainId
	 @throws n/a
	 */
	public int getMinor() {
		return minor;
	}
	
	/**
	 DESCRIPTION:
	 compare two DomainId objects for equality. We consider them equal if
	 the following attributes are the same between the DomainId object.
	 - DID
	 - domainType (or isLogical)
	 - major
	 - minor
	 @return boolean - true means objects are considered the same
	 @throws n/a
	 */
	public boolean equals(DomainId other) {
		boolean rslt = false;      // assume objects are NOT the same
		
		if (this.DID.equals(other.getDID()) && 
			this.domainType.equals(other.getDomainType()) &&
			this.major == other.getMajor() &&
			this.minor == other.getMinor()) {
			rslt = true;
		}
		return rslt;
	}
	
	/**
	 DESCRIPTION:
	 obtain the string representation for this DomainId
	 @return text - this DomainId info in a string for output
	 @throws n/a
	 */
	public String toString() {
		StringBuilder sb = new StringBuilder(128);
		
		sb.append("DomainID -  DID: ");
		sb.append(DID);
		sb.append("\n    Domain Type: ");
		sb.append(domainType);
		sb.append("\n   Display Name: ");
		sb.append(displayName);
		sb.append("\n        Version: ");
		sb.append(version);
		sb.append("\n          Major: ");
		sb.append(major);
		sb.append("\n          Minor: ");
		sb.append(minor);
		return sb.toString();
	}
	
	
	//---------------------------------------------------------------PRIVATE
	private String DID;              // the DID is used for reference in xml
	private String domainType;       // "logical" or "physical"
	private String displayName;
	private String version;
	private int    major;
	private int    minor;
	private boolean isLogical;       // true for logical domain, physical=false
	
}

